package com.itheima.controller;

import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.itheima.common.R;
import com.itheima.dto.DishDto;
import com.itheima.entity.Category;
import com.itheima.entity.Dish;
import com.itheima.entity.DishFlavor;
import com.itheima.service.CategoryService;
import com.itheima.service.DishFlavorService;
import com.itheima.service.DishService;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.BeanUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

import java.util.List;
import java.util.stream.Collectors;

/**
 * @Auther: guozy
 * @Date: 2022/7/31 - 07 - 31 - 16:06
 * @Description: com.itheima.controller
 * @version: 1.0
 */
@Slf4j
@RestController
@RequestMapping("/dish")
public class DishController {

    @Autowired
    private DishService dishService;

    @Autowired
    private DishFlavorService dishFlavorService;

    @Autowired
    private CategoryService categoryService;

    /**
     * 新增菜品
     *
     * @param dishDto
     * @return
     */
    @PostMapping
    public R<String> save(@RequestBody DishDto dishDto) {
        log.info(dishDto.toString());
        dishService.saveDishwithDishFlavor(dishDto);
        return R.success("新增菜品成功");
    }

    /**
     * 分页查询
     *
     * @param page
     * @param pageSize
     * @param name
     * @return
     */
    @GetMapping("/page")
    public R<Page> page(int page, int pageSize, String name) {
        log.info("分页查询,页数={},总页数={},名字={}", page, pageSize, name);
        // 创建分页构造器
        Page<Dish> dishPage = new Page<>(page, pageSize);
        // 创建
        LambdaQueryWrapper<Dish> dqw = new LambdaQueryWrapper<>();
        // 添加过滤条件,没有可不加
        dqw.like(name != null, Dish::getName, name);
        //添加排序条件
        dqw.orderByDesc(Dish::getUpdateTime);
        // 调用查询方法
        dishService.page(dishPage, dqw);
        // 发现问题 新增菜品分类属性不对,传入的数据要改为<DishDto>,为啥不能前面改DTO然后set进去
        Page<DishDto> dishDtoPage = new Page<>();
        BeanUtils.copyProperties(dishPage, dishDtoPage, "records");
        //操作数据,重新赋值
        List<DishDto> dishDtoList = dishPage.getRecords().stream().map((item) -> {
            // 创建个新对象,
            DishDto dishDto = new DishDto();
            // 对象拷贝
            BeanUtils.copyProperties(item, dishDto);
            //通过dish的属性categoryId查询category对象
            Category category = categoryService.getById(item.getCategoryId());
            // 判断对象是否为空
            if (category != null) {
                // 如果不为空,则取出名字,赋值给dishDto
                dishDto.setCategoryName(category.getName());
            }
            return dishDto;
        }).collect(Collectors.toList());
        dishDtoPage.setRecords(dishDtoList);
        return R.success(dishDtoPage);
    }

    /**
     * 通过ID查询菜品数据
     *
     * @return
     */
    @GetMapping("/{id}")
    public R<DishDto> getById(@PathVariable Long id) {
        log.info("通过id{}查询DishDto对象", id);
        // 通过id查询dish数据
        DishDto dishDto = dishService.getByIdWithFlavor(id);
        return R.success(dishDto);
    }

    /**
     * 修改菜品
     *
     * @param dishDto
     * @return
     */
    @PutMapping
    public R<String> updata(@RequestBody DishDto dishDto) {
        log.info(dishDto.toString());
        dishService.updateDishwithDishFlavor(dishDto);
        return R.success("修改菜品成功");
    }


    /**
     * 通过id删除菜品,并删除菜品口味
     *
     * @param ids
     * @return
     */
    @DeleteMapping
    public R<String> delect(@RequestParam List<Long> ids) {
        log.info("ids={}", ids);
        // 删除菜品,并删除菜品口味
        dishService.delectDishwithDishFlavor(ids);
        return R.success("删除菜品成功");
    }

    /**
     * 通过条件查询对应菜品数据
     * @param dish
     * @return
     */

    /*@GetMapping("/list")
    public R<List<Dish>> list(Dish dish) {
        log.info("{}", dish);
        Long categoryId = dish.getCategoryId();
        LambdaQueryWrapper<Dish> queryWrapper = new LambdaQueryWrapper<>();
        queryWrapper.eq(categoryId != 0, Dish::getCategoryId, categoryId)
                .eq(Dish::getStatus, 1);// 状态值为1(启售状态)的菜品

        List<Dish> list = dishService.list(queryWrapper);
        return R.success(list);
    }*/
    @GetMapping("/list")
    public R<List<DishDto>> list(Dish dish) {
        log.info("{}", dish);
        Long categoryId = dish.getCategoryId();
        LambdaQueryWrapper<Dish> queryWrapper = new LambdaQueryWrapper<>();
        queryWrapper.eq(categoryId != 0, Dish::getCategoryId, categoryId)
                .eq(Dish::getStatus, 1);// 状态值为1(启售状态)的菜品

        List<Dish> list = dishService.list(queryWrapper);

        //
        List<DishDto> dishDtoList = list.stream().map((item) -> {
            // 创建个新对象,
            DishDto dishDto = new DishDto();
            // 对象拷贝
            BeanUtils.copyProperties(item, dishDto);
            //通过dish的属性categoryId查询category对象
            Category category = categoryService.getById(item.getCategoryId());
            // 判断对象是否为空
            if (category != null) {
                // 如果不为空,则取出名字,赋值给dishDto
                dishDto.setCategoryName(category.getName());
            }
            //通过菜品id查询菜品口味表 并且写入数据
            Long id = item.getId();
            LambdaQueryWrapper<DishFlavor> lambdaQueryWrapper = new LambdaQueryWrapper<>();
            lambdaQueryWrapper.eq(DishFlavor::getDishId,id);

            List<DishFlavor> dishFlavorList = dishFlavorService.list(lambdaQueryWrapper);
            dishDto.setFlavors(dishFlavorList);
            return dishDto;
        }).collect(Collectors.toList());

        return R.success(dishDtoList);
    }


}
